package com.baizhi.lql.utils;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class AliyunOSSUtil {
    private static String endpoint = "oss-cn-beijing.aliyuncs.com";
    private static String accessKeyId = "LTAI5tL6LLPomwLvKm2jPPGD";
    private static String accessKeySecret = "s4ijW4818kXJecFZpBIQIDXK4t8EIw";

    // 删除文件
    public static void deleteFile(String bucketName,String objectName){
        // 创建OOSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 删除文件或目录，如果要删除目录，目录必须为空
        ossClient.deleteObject(bucketName, objectName);
        // 关闭OSSClient
        ossClient.shutdown();
    }
    // 上传文件
    public static void uploadFileBytes(String bucket, String objectName, MultipartFile headImg) throws IOException {
        // 创建OSSClient实例
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // 填写Byte数组
        byte[] content = headImg.getBytes();
        // 依次填写Bucket名称和Object完整路径 Object完整路径中不能包含bucket名称
        ossClient.putObject(bucket,objectName,new ByteArrayInputStream(content));
        // 关闭OSSClient
        ossClient.shutdown();
    }
}
